<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/css/bus-style.css" media="all">
    <style>
        tr th{text-align: center!important;font-weight: bold!important;}
        tr td{text-align: center!important;}
        .center{text-align: center;background-color: #ccc}
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-card-body">
                        <input type="text" class="layui-input" style="width:40%;float:left;margin-left: 25%" name="proCode"
                               id="proCodeInput" placeholder="请输入商品编码">
                        <button class="layui-btn" data-type="search" lay-submit lay-filter="product-search">查询</button>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="seckill-table" lay-filter="seckill-table"></table>
                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">提交</a>
                    </script>
                </div>
                <hr>
                <div class="layui-card-body" style="margin-top: 20px">
                    <div>
                        <blockquote class="layui-elem-quote" style="font-size: 16px">
                            <span class="red">1. 活动开始前<span style="font-weight: bold"> 15 </span>分钟，不可添加商品。</span><br>
                            2. 输入商品编码，查询商品详情列表。<br>
                            3. 编辑 秒杀价、限购总数、每人限购数、排序，点击【提交】。<br>
                            4. 每人限购数不填，表示不限购。<br>
                            5. 秒杀活动仅限<span style="color: red;font-weight: bold;">【普通商品】</span>参加，非普通商品查询不会展示结果。<br>
                        </blockquote>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="killId" value="${secKill}">
<script src="/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script>
    layui.config({
        //静态资源所在路径
        base: '/layuiadmin/'
    }).extend({
        //主入口模块
        index: 'lib/index'
    }).use(['index', 'form','table'], function(){
        var $ = layui.$,layer = layui.layer,form = layui.form,table=layui.table;

        table.render({
            elem: '#seckill-table',
            url: "/activity/queryProductAjax",
            cols: [[
                {field:'id', title:'ID', width:100, fixed: 'left'},
                {field:'proCode', title:'商品编码', width:140, fixed: 'left'},
                {field:'specName', title:'规格名称', minWidth: 100},
                {field:'price', title:'商品原价', width:90},
                {field:'seckillPrice', title:'秒杀价', edit: 'text', minWidth: 90},
                {field:'totalNum', title:'限购总数',edit: 'text', minWidth: 90},
                {field:'limitNum', title:'每人限购数',edit: 'text', minWidth: 100},
                {field:'sort', title:'排序', edit: 'text', minWidth: 60},
                {width:70, align:'center', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
        });

        form.on('submit(product-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('seckill-table', {
                where: field
            });
        });

        //监听单元格编辑
        table.on('edit(seckill-table)', function(obj){
            //得到修改后的值
            var value = obj.value,oriData = obj.data;
            //得到字段
            var key = obj.field;
            var param = {
                "skuId":oriData.id,
                "proCode":oriData.proCode,
                "seckillPrice":oriData.seckillPrice,
                "totalNum":oriData.totalNum,
                "limitNum":oriData.limitNum,
                "sort":oriData.sort
            };
            param[key] = value;
        });

        table.on('tool(seckill-table)', function(obj){
            var killId = $("#killId").val();
            var data = obj.data;
            if(data.seckillPrice == undefined){
                layer.msg("请输入秒杀价！",{icon:2});
                return;
            }
            if(data.totalNum == undefined){
                layer.msg("请输入限购总数！",{icon:2});
                return;
            }
            if(data.sort == undefined){
                layer.msg("请输入排序！",{icon:2});
                return;
            }
            data.seckillId = killId;
            data.skuId = data.id;
            if(obj.event === 'edit'){
                // console.log(data);
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                $.ajax({
                    url: "/activity/seckillDetailAdd",
                    data: data,
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if (data.status) {
                            layer.msg(data.desc,{icon:1});
                            setTimeout(function () {
                                window.parent.location.reload();
                            }, 1000)
                        } else {
                            layer.msg(data.desc,{icon:2});
                        }
                    },
                    error: function () {
                        layer.msg("请检查页面信息是否正确",{icon:2});
                    }
                });
            }
        });
    });
</script>
</body>
</html>